/*
 * OthersView.java
 *
 * Created on August 23, 2006, 12:40 AM
 */

package com.floreantpos.ui.views.order;

import com.floreantpos.POSConstants;
import com.floreantpos.main.Application;
import com.floreantpos.model.Ticket;
import com.floreantpos.model.TicketItem;
import com.floreantpos.model.dao.TicketDAO;
import com.floreantpos.print.PosPrintService;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.dialog.MiscTicketItemDialog;
import com.floreantpos.ui.dialog.NumberSelectionDialog2;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.ui.dialog.TicketDetailDialog;
import com.floreantpos.ui.views.CookingInstructionView;

/**
 *
 * @author  MShahriar
 */
public class OthersView extends TransparentPanel {
	private Ticket currentTicket;
    
    /** Creates new form OthersView */
    public OthersView() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
        btnOrderInfo = new com.floreantpos.swing.PosButton();
        btnMisc = new com.floreantpos.swing.PosButton();
        btnPrintReceipt = new com.floreantpos.swing.PosButton();
        transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
        btnCustomerNumber = new com.floreantpos.swing.PosButton();
        btnTableNumber = new com.floreantpos.swing.PosButton();
        btnCookingInstruction = new com.floreantpos.swing.PosButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, com.floreantpos.POSConstants.OTHERS, javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        setPreferredSize(new java.awt.Dimension(120, 140));
        setLayout(new java.awt.GridLayout(0, 1, 5, 5));

        transparentPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        transparentPanel2.setPreferredSize(new java.awt.Dimension(140, 50));
        transparentPanel2.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        btnOrderInfo.setText(com.floreantpos.POSConstants.ORDER_INFO);
        btnOrderInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrderInfoActionPerformed(evt);
            }
        });
        transparentPanel2.add(btnOrderInfo);

        btnMisc.setText(com.floreantpos.POSConstants.MISC);
        btnMisc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doInsertMisc(evt);
            }
        });
        transparentPanel2.add(btnMisc);

        btnPrintReceipt.setText(com.floreantpos.POSConstants.PRINT_RECEIPT);
        btnPrintReceipt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintReceiptActionPerformed(evt);
            }
        });
        transparentPanel2.add(btnPrintReceipt);

        add(transparentPanel2);

        transparentPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        transparentPanel1.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        btnCustomerNumber.setText(com.floreantpos.POSConstants.CUSTOMER);
        btnCustomerNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCustomerNumberActionPerformed(evt);
            }
        });
        transparentPanel1.add(btnCustomerNumber);

        btnTableNumber.setText(com.floreantpos.POSConstants.TABLE);
        btnTableNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTableNumberActionPerformed(evt);
            }
        });
        transparentPanel1.add(btnTableNumber);

        btnCookingInstruction.setText("<html><body><center>" + POSConstants.CAPITAL_COOKING_INSTRUCTIONS + "</center></body></html>");
        btnCookingInstruction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCookingInstructionActionPerformed(evt);
            }
        });
        transparentPanel1.add(btnCookingInstruction);

        add(transparentPanel1);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCookingInstructionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCookingInstructionActionPerformed
    	CookingInstructionView cookingInstructionView = new CookingInstructionView(Application.getPosWindow(), true);
    	cookingInstructionView.setTitle(com.floreantpos.POSConstants.COOKING_INSTRUCTIONS);
    	cookingInstructionView.pack();
    	cookingInstructionView.setTicket(getCurrentTicket());
    	cookingInstructionView.open();
    }//GEN-LAST:event_btnCookingInstructionActionPerformed

    private void doInsertMisc(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doInsertMisc
    	MiscTicketItemDialog dialog = new MiscTicketItemDialog(Application.getPosWindow(), true);
    	dialog.open();
    	if(!dialog.isCanceled()) {
    		TicketItem ticketItem = dialog.getTicketItem();
    		RootView.getInstance().getOrderView().getTicketView().addTicketItem(ticketItem);
    	}
    }//GEN-LAST:event_doInsertMisc

    private void btnPrintReceiptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintReceiptActionPerformed
    	try {
			PosPrintService.printTicket(getCurrentTicket(), false);
		} catch (Exception e) {
			POSMessageDialog.showError(Application.getPosWindow(), e.getMessage(), e);
		} 
    }//GEN-LAST:event_btnPrintReceiptActionPerformed

    private void btnOrderInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrderInfoActionPerformed
//    	for (OtherViewListener listener : listeners) {
//			listener.showOrderInfo();
//		}
    	TicketDetailDialog dialog = new TicketDetailDialog(Application.getPosWindow(), true);
    	dialog.setTicket(getCurrentTicket());
    	dialog.open();
    	
    	if(!dialog.isCanceled()) {
    		OrderView.getInstance().getTicketView().updateView();
    	}
    	
    }//GEN-LAST:event_btnOrderInfoActionPerformed

    private void btnCustomerNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCustomerNumberActionPerformed
    	updateGuestNumber();
    }//GEN-LAST:event_btnCustomerNumberActionPerformed
    
    private void updateGuestNumber() {
    	Ticket thisTicket = getCurrentTicket();
    	int guestNumber = thisTicket.getNumberOfGuests();
    	
    	NumberSelectionDialog2 dialog = new NumberSelectionDialog2();
		dialog.setTitle(com.floreantpos.POSConstants.NUMBER_OF_GUESTS);
		dialog.setValue(guestNumber);
		dialog.pack();
		dialog.open();
		
		if(dialog.isCanceled()) {
			return;
		}
		
		guestNumber = (int) dialog.getValue();
		if(guestNumber == 0) {
			POSMessageDialog.showError(Application.getPosWindow(), com.floreantpos.POSConstants.GUEST_NUMBER_CANNOT_BE_0);
			return;
		}
		
    	thisTicket.setNumberOfGuests(guestNumber);
		updateView();

    }

    private void btnTableNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTableNumberActionPerformed
    	updateTableNumber();
    }//GEN-LAST:event_btnTableNumberActionPerformed
    
    private void updateTableNumber() {
    	Ticket thisTicket = getCurrentTicket();
    	int tableNumber = thisTicket.getTableNumber();
    	
    	NumberSelectionDialog2 dialog = new NumberSelectionDialog2();
		dialog.setTitle(com.floreantpos.POSConstants.TABLE_NUMBER);
		dialog.setValue(tableNumber);
		dialog.pack();
		dialog.open();
		
		if(dialog.isCanceled()) {
			return;
		}
		
		tableNumber = (int) dialog.getValue();
		if(tableNumber == 0) {
			POSMessageDialog.showError(Application.getPosWindow(), com.floreantpos.POSConstants.TABLE_NUMBER_CANNOT_BE_0);
			return;
		}
		
		TicketDAO dao = TicketDAO.getInstance();
		Ticket otherTicket = dao.findTicketByTableNumber(tableNumber);
    	
    	if(otherTicket != null && !otherTicket.equals(thisTicket)) {
    		POSMessageDialog.showError(com.floreantpos.POSConstants.TABLE_OCCUPIED);
    		return;
    	}

    	thisTicket.setTableNumber(tableNumber);
		updateView();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.floreantpos.swing.PosButton btnCookingInstruction;
    private com.floreantpos.swing.PosButton btnCustomerNumber;
    private com.floreantpos.swing.PosButton btnMisc;
    private com.floreantpos.swing.PosButton btnOrderInfo;
    private com.floreantpos.swing.PosButton btnPrintReceipt;
    private com.floreantpos.swing.PosButton btnTableNumber;
    private com.floreantpos.swing.TransparentPanel transparentPanel1;
    private com.floreantpos.swing.TransparentPanel transparentPanel2;
    // End of variables declaration//GEN-END:variables
    
    public void updateView() {
    	if(currentTicket != null) {
    		if(currentTicket.getTableNumber() == Ticket.TAKE_OUT) {
    			btnCustomerNumber.setText("");
        		btnTableNumber.setText("");
        		
        		btnCustomerNumber.setEnabled(false);
        		btnTableNumber.setEnabled(false);
    		}
    		else {
    			btnCustomerNumber.setEnabled(true);
        		btnTableNumber.setEnabled(true);
        		
	    		btnCustomerNumber.setText(currentTicket.getNumberOfGuests() + " " + POSConstants.GUEST + "s");
	    		btnTableNumber.setText(POSConstants.TABLE_NO + ": " + currentTicket.getTableNumber());
    		}
    	}
    }

	public Ticket getCurrentTicket() {
		return currentTicket;
	}

	public void setCurrentTicket(Ticket currentTicket) {
		this.currentTicket = currentTicket;
		updateView();
	}
}
